<template>
	<view :class="show?'active':''" catchtouchmove='true'>
		<view class="contents" catchtouchmove='true'>
			<view class="address-window" catchtouchmove='true'>
				<!-- 无地址 -->
				<view class="picture" v-if="lists.length==0">
					<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/car.png"></u-empty>
				</view>
				<scroll-view class="list" v-else scroll-y>
					<u-row gutter="10" v-for="(item,index) in lists" :key="index"
						class="item acea-row row-between-wrapper">
						<u-col span="10">
							<view class="address u-padding-left-30" @click="tapCourse(index,item)">
								<view class="name">{{item[keyName]}}
								</view>
							</view>
						</u-col>
						<u-col span="2" text-align="center">
							<u-icon name="checkbox-mark" color="#fc4141" size="28" v-if="item.checked"></u-icon>
						</u-col>
					</u-row>
				</scroll-view>
				<view class="btn-wrapper">
					<u-button hover-class="other-button-hover" @click="updateUser" data-name="updateinfo" type="warning"
						color="#ff0410">确定</u-button>
				</view>
			</view>
		</view>

		<view class="shadow" v-if="show" @click="close" catchtouchmove='true'></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				lists: [],
				tempLists: []
			}
		},
		props: {
			list: {
				type: Array,
				default: []
			},
			activeList: {
				type: String,
				default: ""
			},
			keyName: {
				type: String,
				default: ""
			},
			show: {
				type: Boolean,
				default: false
			}
		},
		onShow() {
			console.log(this.activeList);
		},
		methods: {
			tapCourse(index, item) {
				if (this.lists[index]['checked']) {
					this.lists[index]['checked'] = false;
					this.tempLists.forEach((itm, idx) => {
						if(itm.hardCode == item.hardCode) {
							this.tempLists.splice(idx, 1);
						}
					})
				} else {
					this.lists[index]['checked'] = true;
					this.tempLists.push(item)
				}
				console.log(this.tempLists);
			},
			close() {
				this.$emit("close");
			},
			updateUser() {
				this.$emit("getActiveLists", this.tempLists);
			},
			findValue(arr, value) {
				let Tvalue = null;
				for (let i = 0; i < arr.length; i++) {
					if (arr[i] == value) {
						// 如果匹配到，返回该对象
						Tvalue = arr[i];
					}
				}
			 return Tvalue;
			}
		},
		mounted() {
			let activeList = this.activeList.length > 0 ? this.activeList.split(",") : [];
			console.log(activeList);
			if(activeList.length > 0) {
				this.list.forEach((item, index) => {
					let Tvalue = this.findValue(activeList, item[this.keyName]);
					if (Tvalue) {
						item["checked"] = true;
						this.tempLists.push(item)
					}
				})
			}
			this.lists = this.list;
		}
	}
</script>

<style lang="scss">

</style>
